package com.wyd.wenyongdaAdmin.admin.menu.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.wyd.wenyongdaAdmin.commons.model.admin.menu.dto.MenuDTO;
import com.wyd.wenyongdaAdmin.commons.model.admin.menu.dto.TreeMenuDTO;
import com.wyd.wenyongdaAdmin.commons.model.admin.menu.vo.VueRouterVO;
import com.wyd.wenyongdaAdmin.commons.util.ResultEnum;
import com.wyd.wenyongdaAdmin.commons.util.ResultT;
import com.wyd.wenyongdaAdmin.commons.util.ResultUtil;
import com.wyd.wenyongdaAdmin.xo.admin.menu.service.IMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    private static final Logger log = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private IMenuService menuService;

    @GetMapping("/get/current/menutree")
    public ResultT<List<TreeMenuDTO>> getCurrentMenu(@RequestParam Long roleId) {
        List<TreeMenuDTO> menu = menuService.getCurrentMenu(roleId);
        if (menu!=null && menu.size() > 0) {
            List<TreeMenuDTO> menuTree = menuService.buildTree(menu);
            return ResultT.<List<TreeMenuDTO>>ok().data(menuTree);
        } else {
            return ResultT.<List<TreeMenuDTO>>ok().data(new ArrayList<>());
        }

    }

    @GetMapping("/get/current/vuerouter")
    public ResultT<List<VueRouterVO>> getCurrentVueRouter() {
        List<TreeMenuDTO> menu = menuService.selectAllMenu();
        List<TreeMenuDTO> menuTree = menuService.buildTree(menu);
        List<VueRouterVO> vueRouterVo = menuService.buildVueRouters(menuTree);
        return ResultT.<List<VueRouterVO>>ok().data(vueRouterVo);
    }

    @GetMapping("/get/menutree")
    public ResultT<List<TreeMenuDTO>> getMenuTree() {
        List<TreeMenuDTO> menu = menuService.selectAllMenu();
        List<TreeMenuDTO> menuTree = menuService.buildTree(menu);
        return ResultT.<List<TreeMenuDTO>>ok().data(menuTree);
    }

    @PostMapping("/save/menutree")
    public ResultUtil addMenuTree(@RequestBody Map<String, Object> reqParams) {
        MenuDTO menuDTO = BeanUtil.fillBeanWithMap(reqParams, new MenuDTO(), false);
        menuDTOIfHasStrBlankToNull(menuDTO);
        if(this.menuService.addMenu(menuDTO) == 0) {
            return ResultUtil.error(ResultEnum.MENU_ERROR_OR_UNKNOWN);
        } else {
            return ResultUtil.ok();
        }
    }

    @PutMapping("/update/menutree")
    public ResultUtil updateMenuTree(@RequestBody Map<String, Object> reqParams) {
        MenuDTO menuDTO = BeanUtil.fillBeanWithMap(reqParams, new MenuDTO(), false);
        menuDTOIfHasStrBlankToNull(menuDTO);
        if (this.menuService.updateMenu(menuDTO) == 0) {
            return ResultUtil.error(ResultEnum.MENU_ERROR_OR_UNKNOWN);
        } else {
            return ResultUtil.ok();
        }
    }

    @DeleteMapping("/delete/menutree")
    public ResultUtil deleteMenuTree(@RequestBody Map<String, Object> reqParams) {
        Long id = Long.valueOf(reqParams.get("id").toString());
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        if (this.menuService.deleteMenu(ids) == 0) {
            return ResultUtil.error(ResultEnum.MENU_ERROR_OR_UNKNOWN);
        } else {
            return ResultUtil.ok();
        }
    }

    private void menuDTOIfHasStrBlankToNull(MenuDTO menuDTO) {
        if (StrUtil.isBlank(menuDTO.getEnname())) {
            menuDTO.setEnname(null);
        }
        if (StrUtil.isBlank(menuDTO.getPath())) {
            menuDTO.setPath(null);
        }
        if (StrUtil.isBlank(menuDTO.getComponent())) {
            menuDTO.setComponent(null);
        }
        if (StrUtil.isBlank(menuDTO.getIcon())) {
            menuDTO.setIcon(null);
        }
    }
}
